package util

import (
	"github.com/BurntSushi/toml"
	"sync"
	"time"
)
type tomlConfig struct {
	Title string `toml:"title"`
	RunMode string `toml:"run_mode"`
	App app `toml:"app"`
	DB database `toml:"database"`
	Server servers `toml:"servers"`
	Zstack zstack `toml:"zstack"`
}
type app struct {
	PageSize int64 `toml:"page_size"`
	JwtSecret string `toml:"jwt_secret"`
	Salt string `toml:"salt"`
}
type database struct {
	Type string `toml:"type"`
	User string `toml:"user"`
	Password string `toml:"password"`
	Host string `toml:"host"`
	DbName string `toml:"db_name"`
	TablePrefix string `toml:"table_prefix"`
	Mode bool `toml:"mode"`
}
type servers struct {
	HttpPort string `toml:"http_port"`
	HtmlHttpPort string `toml:"html_http_port"`
	ReadTimeout time.Duration `toml:"read_timeout"`
	WriteTimeout time.Duration `toml:"write_timeout"`
}
type zstack struct{
	ApiUrl string `toml:"api_url"`
	WebHook string `toml:"web_hook"`
	UserName string `toml:"username"`
	Password string `toml:"password"`
}

var cfg *tomlConfig
var once sync.Once

func config() *tomlConfig{

	//file, _ := exec.LookPath(os.Args[0])
	//path, _ := filepath.Abs(file)
	//index := strings.LastIndex(path, string(os.PathSeparator))
	//path = path[:index] + "/config/conf.toml"

	path := "./config/conf.toml"
	once.Do(func(){
		if _,err := toml.DecodeFile(path,&cfg);err!=nil{
			panic(err)
		}
	})
	return cfg
}
var Config = config()